﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
    CustomerBus cusBus = new CustomerBus();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) 
        {
            loadCustomer();
        }
        
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        Customer cus = new Customer();
        cus.First_name = txtFirstName.Text;
        cus.Last_name = txtLastName.Text;
        cus.Address = txtAddress.Text;
        cus.City = txtCity.Text;
        cus.Phone = txtPhone.Text;
        cus.Email = Convert.ToString(Session["username"]);
        cus.Customer_group_id = Convert.ToInt32(Session["customer_group_id"]);
        if (cusBus.InsertUpdateCustomer(cus))
        {
            ScriptManager.RegisterStartupScript(this, typeof(Page), "Test.com", "<script>alert('Update Customer successfully')</script>", false);
        }
        else
        {
            ScriptManager.RegisterStartupScript(this, typeof(Page), "Test.com", "<script>alert('Update Customer Fail')</script>", false);
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("default.aspx");
    }
    public void loadCustomer()
    {
        if (Session["username"] == "" || Session["username"] == null)
        {
            Response.Redirect("default.aspx");
        }
        else
        {
            string email = Convert.ToString(Session["username"]);
            Customer cus = cusBus.getCustomerByEmail(email);
            txtFirstName.Text = cus.First_name;
            txtLastName.Text = cus.Last_name;
            txtAddress.Text = cus.Address;
            txtCity.Text = cus.City;
            txtPhone.Text = cus.Phone;
        }

    }
}